/**
 * 
 */
package com.imonsoft.common.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.OncePerRequestFilter;

import com.imonsoft.common.util.ContextUtil;

/**
 * 
 * 将资源存入ThreadLocal中，以便用户可以在任何一层中获得
 * 
 * @author LiQianHui
 * @date 2013-04-17
 * @version 2013
 */
public class OpenContextInTierFilter extends OncePerRequestFilter {

	@Override
	protected void doFilterInternal(HttpServletRequest request,
			HttpServletResponse response, FilterChain filterChain)
			throws ServletException, IOException {
		String uri = request.getRequestURI();
		uri = uri.toLowerCase();
		if (uri.endsWith(".jpg") || uri.endsWith(".png")
				|| uri.endsWith(".gif") || uri.endsWith(".js")
				|| uri.endsWith(".css") || uri.endsWith(".html")
				|| uri.endsWith(".htm") || uri.endsWith(".xml")
				|| uri.endsWith(".htc")) {
			filterChain.doFilter(request, response);
		} else {
			try {
				ContextUtil.setWebContext(request, response,getServletContext());
				filterChain.doFilter(request, response);
			} finally {
				ContextUtil.unset();
			}
		}
	}

}
